var express = require('express');
const mysql_query = require("../common/mysql");
const httpresult = require("../common/response");
var router = express.Router();
// var mysql = require('mysql');
// var mysql_conn = mysql.createConnection({
//   host     : 'localhost',
//   user     : 'root',
//   password : '123456',
//   port: '3306',
//   database: 'test'
// });
//
// mysql_conn.connect();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/list', async function(req, res, next) {
  var menus_list = [];
  menus_list.push(
      {
        path: "/home/index", // 浏览器上面显示的 url 地址
        name: "home",
        component: "/home/index",// vue 文件路径
        meta: {
          icon: "HomeFilled",
          title: "首页",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: true,
          isKeepAlive: true
        }
      },
      {
        path: "/my_study",
        name: "my_study",
        component: "/my_study",
        meta: {
          icon: "Tickets",
          title: "我的学习",
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: false
        },
        children: [
          {
            path: "/my_study/study_record",
            name: "studyRecord",
            component: "/my_study/study_record/index",
            meta: {
              icon: "Menu",
              title: "学习记录",
              isLink: "",
              isHide: false,
              isFull: false,
              isAffix: false,
              isKeepAlive: false
            },
          }
        ]
      },);
  if (req.user_role === "admin") {
    menus_list.push({
      path: "/admin",
      name: "admin",
      component: "/admin",
      meta: {
        icon: "Management",
        title: "管理模块",
        isLink: "",
        isHide: false,
        isFull: false,
        isAffix: false,
        isKeepAlive: false
      },
      children: [
        {
          path: "/admin/course_manage",
          name: "courseManage",
          component: "/admin/course_manage/index",
          meta: {
            icon: "Menu",
            title: "科目管理",
            isLink: "",
            isHide: false,
            isFull: false,
            isAffix: false,
            isKeepAlive: false
          },
        },
        {
          path: "/admin/user_manage",
          name: "userManage",
          component: "/admin/user_manage/index",
          meta: {
            icon: "Menu",
            title: "用户管理",
            isLink: "",
            isHide: false,
            isFull: false,
            isAffix: false,
            isKeepAlive: false
          }
        },
      ]
    })
  }
  menus_list.push(
      {
        path: "/bank",
        name: "bank",
        component: "/bank",
        meta: {
          icon: "Management",
          title: "题库模块",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: false
        },
        children: [
          {
            path: "/bank/editBankQuestion/:bank_info",
            name: "editBankQuestion",
            component: "/bank/editBankQuestion",
            meta: {
              icon: "Menu",
              title: "编辑试卷",
              isLink: "",
              isHide: false,
              isFull: false,
              isAffix: false,
              isKeepAlive: false
            },
          },
          {
            path: "/admin/editBank/:bank_id",
            name: "editBank",
            component: "/admin/editBank/index",
            meta: {
              icon: "Menu",
              title: "编辑试题",
              isLink: "",
              isHide: false,
              isFull: false,
              isAffix: false,
              isKeepAlive: false
            },
          }
        ]
      },
      {
        path: "/question",
        name: "question",
        component: "/question",
        meta: {
          icon: "Management",
          title: "试题模块",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: false
        },
        children: [
          {
            path: "/question/browse/:condition",
            name: "questionBrowse",
            component: "/question/browse",
            meta: {
              icon: "Menu",
              title: "浏览试题",
              isLink: "",
              isHide: false,
              isFull: false,
              isAffix: false,
              isKeepAlive: false
            },
          }
        ]
      },
      {
        path: "/chapter/index/:course_id",
        name: "chapter",
        component: "/chapter/index",
        meta: {
          icon: "Management",
          title: "章节管理",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: false
        },
      },
      {
        path: "/exercise",
        name: "exercise",
        component: "/exercise",
        meta: {
          icon: "Management",
          title: "背知识点模块",
          isLink: "",
          isHide: true,
          isFull: false,
          isAffix: false,
          isKeepAlive: false
        },
        children: [
          {
            path: "/exercise/index/:bank_id",
            name: "excercise",
            component: "/exercise/index",
            meta: {
              icon: "Menu",
              title: "背知识点",
              isLink: "",
              isHide: true,
              isFull: false,
              isAffix: false,
              isKeepAlive: false
            },
          }
        ]
      });
  var [error, data] = await mysql_query(
      "select user_course.course_id, name, icon, isLink, isHide, isFull, isAffix, isKeepAlive, redirect, parent_id, activeMenu, title, order_number  from user_course " +
      "left join course on user_course.course_id = course.course_id where user_id = ?", [req.user_id]);
  if (error) {
    logger.warn(error);
    return res.send(httpresult.sys_error(error));
  }
  data = data.sort((a, b) => {
    return a.order_number - b.order_number;
  })
  var menus = {};
  for (let index in data) {
    if (data[index]["parent_id"] > 0) {
      data[index]["path"] = '/course/' + data[index]["course_id"];
      // data[index]["path"] = '/course/:course_id';
      data[index]["component"] = '/course/index';
      // data[index]["children"] = [
      //     {
      //         "path": "/course/chapter/:id",
      //         "name": "chapter",
      //         "component": "/course/chapter/index",
      //         "meta": {
      //             "icon": "Menu",
      //             "title": "Tab 详情",
      //             "activeMenu": "/assembly/tabs",
      //             "isLink": "",
      //             "isHide": true,
      //             "isFull": false,
      //             "isAffix": false,
      //             "isKeepAlive": true
      //         }
      //     }
      // ]
    } else {
      data[index]["path"] = '/' + data[index]["course_id"];
      data[index]["component"] = '/';
    }

    data[index]["meta"] = {
      icon: data[index].icon,
      isLink: data[index].isLink,
      isHide: data[index].isHide,
      isFull: data[index].isFull,
      isAffix: data[index].isAffix,
      isKeepAlive: data[index].isKeepAlive,
      activeMenu: data[index].activeMenu,
      title: data[index].title
    };
    data[index].icon = undefined;
    data[index].isLink = undefined;
    data[index].isHide = undefined;
    data[index].isFull = undefined;
    data[index].isAffix = undefined;
    data[index].isKeepAlive = undefined;
    data[index].activeMenu = undefined;
    data[index].title = undefined;

    if (data[index].parent_id === 0) {
      menus[data[index].course_id] = data[index];
      menus[data[index].course_id]["children"] = [];
    }
  }

  for (let index in data) {
    if (data[index].parent_id !== 0 && menus.hasOwnProperty(data[index].parent_id)) {
      menus[data[index].parent_id].children.push(data[index]);
    }
  }
  for (let index in menus) {
    if(menus[index].children.length === 0) {
      menus[index].children = undefined;
    }
    menus_list.push(menus[index]);
  }
  return res.send(httpresult.success(menus_list));
});
module.exports = router;
